package com.javaframe.role.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;



import com.javaframe.role.dao.RoleDAO;
import com.javaframe.role.entity.Role;
import com.javaframe.role.tools.ConditionInfo;
import com.javaframe.role.tools.GenericsUtils;
import com.javaframe.role.tools.PageInfo;

@Service
public class RoleServiceImpl implements RoleService {

	RoleDAO dao;

	@Autowired
	public void setDao(RoleDAO dao) {
		this.dao = dao;
	}
	
	public void save(Role Role) {
		dao.save(Role);
	}

	public List<Role> findAll() {
		return dao.getAll();
	}

	public Role findById(Integer id) {
		return dao.get(id);
	}

	public void delete(Integer id) {
		dao.removeById(id);
	}
	
	public void delete(Map<?,?> map) {
		dao.removeByMap(map);
	}

	public List<Role> findAll(List<ConditionInfo> conditions,PageInfo page) {
		StringBuffer hql = new StringBuffer(" select new map(a.roleId as roleId, a.roleName as roleName) from Role a where a.deleted = 0");
		List<?> list = new ArrayList<Object>(); 
		if(conditions != null) {
	         for (ConditionInfo info : conditions) {
	            hql.append(GenericsUtils.buildHqlCondition(info.getAlias() + info.getConditionName(), info.getConditionValue(), info.getAlgorithm(), list));
	         }
	      }
	      if (page != null && !GenericsUtils.isEmpty(page.getOrderByObj())) {
	         hql.append(" order by ").append(page.getOrderByObj()).append(" ").append(page.getOrderBy()).append(", roleId");
	      } else {
	         hql.append(" order by roleId");
	      }
		return dao.getAll(hql.toString(), list, page);
	}

}
